package services;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.WebHraDetail;
import models.mappers.DeviceMapper;
import models.mappers.WebHraDetailMapper;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.joda.time.DateTime;

import vos.HraDetailSearchVo;
import vos.HraDetailView;
import vos.SjbbSearchVo;
import vos.SjbbVo;
import vos.UserInfoSearchVo;

import com.aton.db.SessionFactory;

public class HraDetailService {
	/**
	 * 统计列表行数
	 *
	 * @Title: countdetail 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countdetail(HraDetailSearchVo vo){
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.countHraDetail(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据筛选条件查询列表数据
	 *
	 * @Title: listHraDetail 
	 * @param vo 筛选条件
	 * @return
	 * @return: List<HraDetailView>
	 */
	public static List<HraDetailView> listHraDetail(HraDetailSearchVo vo) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.listHraDetail(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据主键查体检详细信息
	 *
	 * @Title: getDetail 
	 * @param id 身份证号
	 * @return
	 * @return: HraDetailView
	 */
	public static HraDetailView getDetail(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.getDetail(id);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据主键获取pdf名称
	 *
	 * @Title: getPdfName 
	 * @param did 主键
	 * @return
	 * @return: HraDetailView
	 */
	public static String getPdfName(String did) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.getPdfName(did);
        } finally {
            ss.close();
        }
	}
	/**
	 * 查询今日上传数据的设备id集合
	 *
	 * @Title: listNewHraids 
	 * @param date
	 * @return
	 * @return: List<String>
	 */
	public static List<String> listNewHraids(DateTime date) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
			String datetime=date.toString("yyyy-MM-dd");
			String starttime=datetime+" 00:00:00";
			String endtime=datetime+" 23:59:59";
            return mapper.listNewHraids(starttime,endtime);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据用户身份证号统计体检次数
	 *
	 * @Title: countByIdCard 
	 * @param idcard
	 * @return
	 * @return: long
	 */
	public static long countByIdCard(String idcard) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.countByIdCard(idcard);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据用户身份证号获取体检数据列表
	 *
	 * @Title: listByIdCard 
	 * @param vo 身份证号
	 * @return
	 * @return: List<HraDetailView>
	 */
	public static List<HraDetailView> listByIdCard(UserInfoSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.listByIdCard(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 设置为不统计
	 *
	 * @Title: concleCount 
	 * @param listid
	 * @param operatorid
	 * @return
	 * @return: boolean
	 */
	public static boolean concleCount(List<String> listid, String operatorid) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            mapper.concleCount(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 启用统计
	 *
	 * @Title: Count 
	 * @param listid
	 * @param operatorid
	 * @return
	 * @return: boolean
	 */
	public static boolean Count(List<String> listid, String operatorid) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            mapper.Count(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 查询体检人员是否存在统计记录
	 *
	 * @Title: getTimeCount 
	 * @param idcard
	 * @param datetime 
	 * @return
	 * @return: WebHraDetail
	 */
	public static String getTimeCount(String idcard, String datetime,String enddate) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.getTimeCount(idcard,datetime,enddate);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据设备统计体检次数
	 *
	 * @Title: countByDevice 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countByDevice(HraDetailSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.countByDevice(vo.deviceid);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据设备查询体检数据
	 *
	 * @Title: listByDevice 
	 * @param vo
	 * @return
	 * @return: List<HraDetailView>
	 */
	public static List<HraDetailView> listByDevice(HraDetailSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.listByDevice(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据设备、经销商、门店统计合格数据
	 *
	 * @Title: countStandard 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countStandard(SjbbSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.countStandard(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据设备、经销商、门店获取合格数据
	 *
	 * @Title: getStandard 
	 * @param vo
	 * @return
	 * @return: List<HraDetailView>
	 */
	public static List<HraDetailView> getStandard(SjbbSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.getStandard(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据设备、经销商、门店统计所有的数据
	 *
	 * @Title: countAll 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countAll(SjbbSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.countAll(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据设备、经销商、门店获取所有的数据
	 *
	 * @Title: countAll 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static List<HraDetailView> getAll(SjbbSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.getAll(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取今天体检数据
	 *
	 * @Title: getTodayCount 
	 * @param today
	 * @return
	 * @return: List<Map<String,String>>
	 */
	public static List<SjbbVo> getTodayCount(String today) {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(today)){
			return null;
		}
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.getTodayCount(today);
        } finally {
            ss.close();
        }
	}
	/**
	 * 统计未上传的报告
	 *
	 * @Title: countNotUpload 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countNotUpload(HraDetailSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.countNotUpload(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取未上传的列表
	 *
	 * @Title: listNotUpload 
	 * @param vo
	 * @return
	 * @return: List<HraDetailView>
	 */
	public static List<HraDetailView> listNotUpload(HraDetailSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
            return mapper.listNotUpload(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 更新是否上传
	 *
	 * @Title: updateIsUpload 
	 * @param did
	 * @return: void
	 */
	public static void updateIsUpload(String did) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
			mapper.updateIsUpload(did);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
		}
		finally{
			ss.close();
		}
	}
	/**
	 * 导出excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<HraDetailView>
	 */
	public static List<HraDetailView> exportXls(HraDetailSearchVo vo) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			WebHraDetailMapper mapper = ss.getMapper(WebHraDetailMapper.class);
			return mapper.exportXls(vo);
		}finally{
			ss.close();
		}
	}
}
